package com.example.sjjg.utest.view

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.sjjg.utest.R
import com.example.sjjg.utest.databinding.MainItemBinding

class DataAdapter : RecyclerView.Adapter<DataAdapter.DataHolder>() {

    lateinit var data : ArrayList<Int>

    class DataHolder (item : View): RecyclerView.ViewHolder(item){
        val binding = MainItemBinding.bind(item)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataHolder {
        val inflater = LayoutInflater.from(parent.context)
        val v = inflater.inflate(R.layout.main_item,parent,false)
        val holder = DataHolder(v)
        return holder
    }

    override fun onBindViewHolder(holder: DataHolder, position: Int) {
        val value = if (::data.isInitialized) data[position] else 0
        holder.binding.index = position
        holder.binding.value = value
    }

    override fun getItemCount() = if (::data.isInitialized) data.size else 0
}